package com.bsoft.gol.hbs.service.impl;

import com.bsoft.gol.hbs.dao.BaseTransferPlatformDAO;
import com.bsoft.gol.hbs.dao.BaseTransferRelationDAO;
import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.BaseTransferPlatform;
import hcn.base.BaseTransferRelation;
import hcn.base.Organization;
import hcn.base.drug.vo.BaseTransferVo;
import hcn.common.ContextHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import service.rpc.OrganizationInfoIntf;

import java.util.Date;
import java.util.List;

/**
 * Created by pangxl on 2019/12/31.
 */
@SsdevService("baseTransferService")
public class BaseTransferServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(BaseTransferServiceImpl.class);
    @Autowired
    private BaseTransferPlatformDAO baseTransferPlatformDAO;
    @Autowired
    private BaseTransferRelationDAO baseTransferRelationDAO;
    @Autowired
    private OrganizationInfoIntf organizationInfoService;
    /**
     * 新增、编辑 流转平台
     * @param platform
     * @param relation
     */
    @RpcService
    public void saveBaseTransferPlatform(BaseTransferPlatform platform, BaseTransferRelation relation) throws ControllerException {
        String uid = ContextHelper.getCurrentUserId();
        if("1".equals(relation.getStatus())){
            relation.setEnableDt(new Date());
            relation.setEnableUser(uid);
        }
        //先判断平台（一个链接地址代表一个平台）是否存在，如果已存在不需要重新添加平台，直接新增关联关系。
        BaseTransferPlatform pf = baseTransferPlatformDAO.getBaseTransferPlatform(platform.getPlatformUrl());
        if(CommonUtils.isEmpty(pf)) {
            pf=savePlatform(platform);
        }
        BaseTransferRelation rel = baseTransferRelationDAO.getBaseTransferRelation(pf.getPlatformId(), relation.getOrgId());
        //新增
        if(CommonUtils.isEmpty(relation.getRelationId())) {
            if (CommonUtils.isEmpty(rel)) {
                saveRelation(relation,pf.getPlatformId());
            }else{
                throw new ControllerException(0,"平台链接不能重复!");
            }
        }else{
            //编辑
            BaseTransferRelation rt = baseTransferRelationDAO.get(relation.getRelationId());
            rt.setPlatformId(pf.getPlatformId());
            rt.setModifyDt(new Date());
            rt.setModifyUser(uid);
            rt.setTransferType(relation.getTransferType());
            rt.setStatus(relation.getStatus());
            rt.setPlatformName(relation.getPlatformName());
            if(!platform.getPlatformId().equals(pf.getPlatformId())){//平台链接有修改
                 if(CommonUtils.isNotEmpty(rel)){
                     throw new ControllerException(0,"平台链接不能重复!");
                 }
            }
            baseTransferRelationDAO.update(rt);
        }
    }
    //保存关联
    public BaseTransferRelation saveRelation(BaseTransferRelation relation, int platformId) {
        String uid = ContextHelper.getCurrentUserId();
        relation.setCreateDt(new Date());
        relation.setCreateUser(uid);
        relation.setDeleteFlag("0");
        relation.setEnableDt(new Date());
        relation.setEnableUser(uid);
        relation.setPlatformId(platformId);
        BaseTransferRelation rt=baseTransferRelationDAO.save(relation);
        return rt;

    }

    //保存平台
    public BaseTransferPlatform savePlatform(BaseTransferPlatform platform) {
        String uid = ContextHelper.getCurrentUserId();
        platform.setCreateDt(new Date());
        platform.setCreateUser(uid);
        platform.setDeleteFlag("0");
        platform.setEnableDt(new Date());
        platform.setEnableUser(uid);
        BaseTransferPlatform pf = baseTransferPlatformDAO.save(platform);
        return pf;

    }


    /**
     * 修改机构流转方式
     * @param orgId
     * @param transferFlag
     */
    @RpcService
    public void saveOrgTransferFlag(String orgId,String transferFlag) throws ControllerException {
        Organization organization = organizationInfoService.getOrgDetailByOrgId(orgId);
        if(null != organization){
            organization.setTransferFlag(transferFlag);
        }
        organizationInfoService.update(organization);
    }

    /**
     * 禁用、启用
     * @param relationId
     * @param status
     */
    @RpcService
    public void enableBaseTransferPlatform(Integer relationId,String status){
        String uid = ContextHelper.getCurrentUserId();
        BaseTransferRelation relation = baseTransferRelationDAO.get(relationId);
        if("1".equals(status)){
            relation.setEnableUser(uid);
            relation.setEnableDt(new Date());
        }
        relation.setStatus(status);
        relation.setModifyDt(new Date());
        relation.setModifyUser(uid);
        baseTransferRelationDAO.update(relation);
    }

    @RpcService
    public List<BaseTransferVo> findTransferPlatformList(String orgId){
        List<BaseTransferVo> list = baseTransferPlatformDAO.queryBaseTransfePlatformrList(orgId);
        return list;

    }

    /**
     * 获取机构流转方式
     * @param orgId
     */
    @RpcService
    public String getOrgTransferFlag(String orgId) throws ControllerException {
        Organization organization = organizationInfoService.getOrgDetailByOrgId(orgId);
        String transferFlag = null;
        if(null != organization){
            transferFlag = organization.getTransferFlag();
        }

        if(CommonUtils.isEmpty(transferFlag)){
            transferFlag="0";
        }
        return transferFlag;
    }

    public BaseTransferPlatformDAO getBaseTransferPlatformDAO() {
        return baseTransferPlatformDAO;
    }

    public void setBaseTransferPlatformDAO(BaseTransferPlatformDAO baseTransferPlatformDAO) {
        this.baseTransferPlatformDAO = baseTransferPlatformDAO;
    }

    public BaseTransferRelationDAO getBaseTransferRelationDAO() {
        return baseTransferRelationDAO;
    }

    public void setBaseTransferRelationDAO(BaseTransferRelationDAO baseTransferRelationDAO) {
        this.baseTransferRelationDAO = baseTransferRelationDAO;
    }

}
